package br.com.drestranho.escola.dao;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import br.com.drestranho.escola.entity.DisciplinaNome;

@Stateless
public class DisciplinaNomeDAO {
       @PersistenceContext(unitName="H6Escola")
       private EntityManager em;
       
       public void salvar(DisciplinaNome bean) {
               em.persist(bean);
       }
       
       public void atualizar(DisciplinaNome bean) {
               em.merge(bean);
       }
       
       public void remover( DisciplinaNome bean) {
               em.remove(em.merge(bean));
       }
       
       public DisciplinaNome getBean(Integer id) {
               DisciplinaNome bean = em.find(DisciplinaNome.class, id);
               return bean;
       }
       
       public List<DisciplinaNome> getBeans(){//Lista
               CriteriaBuilder cb = em.getCriteriaBuilder();
               CriteriaQuery<DisciplinaNome> cq = cb.createQuery(DisciplinaNome.class);
               Root<DisciplinaNome> from = cq.from(DisciplinaNome.class);
               CriteriaQuery<DisciplinaNome> select = cq.select(from);
               List<DisciplinaNome> Disciplinas = em.createQuery(select).getResultList();
               return Disciplinas;
       }
       
       public List<String> getNomes() {
    	   Query qry = em.createQuery("select distinct d.nome from DisciplinaNome d");
    	   ArrayList<String> nomes = (ArrayList<String>)qry.getResultList();
    	   return nomes;
       }
       
}   
